package com.example.apione.model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.apione.dao.ApiAccesslogDao;
import com.example.apione.dao.ApiInfoDao;
import com.example.apione.entity.ApiAccesslog;
import com.example.apione.entity.ApiInfo;
import com.example.apione.utils.ResponseResult;

import java.util.HashMap;
import java.util.List;

/**
 * @author 尘落
 * @date 2023/2/24
 */
public class countApimodel {
    public static HashMap<String,Integer> countApi(ApiInfoDao apiInfoDao){
        int sum=0;
        QueryWrapper<ApiInfo> wrapper = new QueryWrapper<>();
        wrapper.select("count");
        List<ApiInfo> allApiList = apiInfoDao.selectList(wrapper);
        for (ApiInfo data:allApiList) {
             sum+=data.getCount();
        }
        Integer ApiSum = Math.toIntExact(apiInfoDao.selectCount(null));
        HashMap<String,Integer> data =new HashMap<>();
        data.put("allcount",sum);
        data.put("apisum",ApiSum);
        return data;
    }

    public static HashMap<String,Integer> countAccess(ApiAccesslogDao apiAccesslogDao, long day1, long day5){
        long sum=0;
        QueryWrapper<ApiAccesslog> wrapper = new QueryWrapper<>();
        wrapper.between("create_time",day1,day5);
        sum =apiAccesslogDao.selectCount(wrapper);
        Integer ApiSum = Math.toIntExact(apiAccesslogDao.selectCount(null));
        HashMap<String,Integer> data =new HashMap<>();
        data.put("allcount", (int) sum);
        data.put("apisum",ApiSum);
        return data;
    }

    public static  ResponseResult<List<ApiInfo>> getApiAccessList(ApiInfoDao apiInfoDao){
        QueryWrapper<ApiInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("count").last("limit 9");
        List<ApiInfo> AccessList =apiInfoDao.selectList(wrapper);
        return ResponseResult.ok(AccessList);
    }
}
